//
//  HomeViewController.m
//  YongChuanTradingArea
//
//  Created by 李冰 on 2017/9/8.
//  Copyright © 2017年 李冰. All rights reserved.
//

#import "HomeViewController.h"
#import "UINavigationBar+LBExtend.h"
#import "HomeDataModel.h"

#import "InputCodeVerifyController.h" // 输码验证
//#import "RecordedCostController.h" // 买单收银
#import "MineMediaPromoteViewController.h" // 买单收银
#import "OrderManagerController.h" // 订单管理
#import "MonthReportController.h" // 月报
#import "WordOfMouthController.h" // 口碑管理
#import "ScanViewController.h" // 扫描二维码
#import "StoreOrderDetailController.h" // 订单详情


@interface HomeViewController ()
@property (weak, nonatomic) IBOutlet UIView *bannerContent;
@property (weak, nonatomic) IBOutlet UILabel *storeCount;
@property (weak, nonatomic) IBOutlet UILabel *goodsCount;
@property (weak, nonatomic) IBOutlet UILabel *waitUseCount;
@property (weak, nonatomic) IBOutlet UILabel *refundCount;
@property (weak, nonatomic) IBOutlet UILabel *verifyCount;

@property (nonatomic, strong) HomeDataModel *model;

@end

@implementation HomeViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    self.view.backgroundColor = kBgColor;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.titleView = [UIView new];
    
    SDCycleScrollView *scroll = [[SDCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, lbScreenWidth, AdaptationRatio(209))];
    [_bannerContent addSubview:scroll];
    scroll.localizationImageNamesGroup = @[@"banner_01", @"banner_02", @"banner_03", @"banner_04"];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBar.lb_hidden = YES;
    
    [self _getHomeData];
    
    _storeCount.text = [LBUserManage defaultManager].userModel.MEMBERNAME;
    
}

- (void) _getHomeData {

    [LBNetworkingInstance requestGET:memberHomeStatistics parameters:@{@"memberId" : [LBUserManage defaultManager].memberId} showHUD:YES success:^(id responseObject, NSInteger code, NSString *message) {
        
        if (code == YCNetworkingStatusCodeSuccess)
        {
            _model = [HomeDataModel yy_modelWithJSON:responseObject[@"Data"]];
            [self _updateUI];
        }
        
    } failure:^(NSError *err) {
        
    }];
}

- (void) _updateUI {

//    _storeCount.text = _model.STORE;
    _goodsCount.text = _model.GOODS;
    _waitUseCount.text = _model.COUPONNOORDER;
    _refundCount.text = _model.RETURNORDER;
    _verifyCount.text = _model.VALIDATEORDER;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar lb_reset];
    
//    [LBNetworkingInstance requestGET:getMenberInfo parameters:@{@"id":[LBUserManage defaultManager].user_id?:@""} showHUD:YES success:^(id  _Nonnull responseObject, NSInteger code, NSString * _Nonnull message) {
//        if (code == YCNetworkingStatusCodeSuccess){
//            UserModel *userModel = [UserModel yy_modelWithJSON:responseObject[@"Data"]];
//            [LBUserManage defaultManager].userModel = userModel;
//
//
//
//        }
//    }];
    
}

#pragma mark - action

- (IBAction)inputCodeVerifyAction:(UIButton *)sender {
    
    InputCodeVerifyController *vc = [InputCodeVerifyController new];
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)scanCodeVerifyAction:(UIButton *)sender {
    
    ScanViewController *vc = [ScanViewController new];
    vc.pushBlock = ^(NSString *scanString, UIViewController *vc) {
      
        [vc dismissViewControllerAnimated:YES completion:nil];
        [self handleQRCode:scanString];
    };
    [self presentViewController:vc animated:YES completion:nil];
    
}
- (IBAction)payAction:(UIButton *)sender {
    
//    RecordedCostController *vc = [RecordedCostController new];
//    [self.navigationController pushViewController:vc animated:YES];
    MineMediaPromoteViewController *vc = [MineMediaPromoteViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)orderManagerAction:(UIButton *)sender {
    
    OrderManagerController *vc = [OrderManagerController new];
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)weekReportAction:(UIButton *)sender {
    
    MonthReportController *vc = [MonthReportController new];
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)monthReportAction:(UIButton *)sender {
    
    MonthReportController *vc = [MonthReportController new];
    vc.isMonth = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)goodCountAction:(UIButton *)sender {
    
//    WordOfMouthController *vc = [WordOfMouthController new];
//    [self.navigationController pushViewController:vc animated:YES];
}

- (void) handleQRCode:(NSString *) QRCode {

    [LBNetworkingInstance requestGET:scanCodeVerify parameters:@{@"userPwd":QRCode} showHUD:YES success:^(id  _Nonnull responseObject, NSInteger code, NSString * _Nonnull message) {
        
        if (code == YCNetworkingStatusCodeSuccess)
        {
            StoreOrderDetailModel *model = [StoreOrderDetailModel yy_modelWithJSON:responseObject[@"Data"]];
            StoreOrderDetailController *vc = [StoreOrderDetailController new];
            vc.model = model;
            vc.QRCode = QRCode;
            [self.navigationController pushViewController:vc animated:YES];
        }
        
    } failure:^(NSError * _Nonnull err) {
        
    }];
}

- (UIStatusBarStyle)preferredStatusBarStyle {

    return UIStatusBarStyleLightContent;
}

@end
